package com.hdjk.view.network;

import com.hdjk.view.callback.AddShopCarCallback;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.callback.DelShopCarCallback;
import com.hdjk.view.callback.EditShopCarCallback;
import com.hdjk.view.callback.GetShopCarListCallBack;
import com.hdjk.view.config.Constant;
import com.lst.projectlib.entity.Msg;
import com.zhy.http.okhttp.OkHttpUtils;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * Created by sxy on 2018/1/2.
 */

public class ShopCarNetWork {
    /**
     * 获取购物车列表
     * @param listener
     */
    public static void getShopCarList(String token, final CallBackListener listener) {
        try {
            OkHttpUtils.get().url(Constant.URL_SHOPCAR_LIST)
                    .addHeader("Authorization","Bearer "+token)
                    .build()
                    .execute(new GetShopCarListCallBack() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }


    /**
     * 加入购物车
     * @param jsonInfo
     * @param listener
     */
    public static void addShopCar(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_SHOPCAR_ADD)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new AddShopCarCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 取消购物车
     * @param jsonInfo
     * @param listener
     */
    public static void delShopCar(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_SHOPCAR_DEL)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new DelShopCarCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

    /**
     * 修改购物车
     * @param jsonInfo
     * @param listener
     */
    public static void editShopCar(String token,String jsonInfo, final CallBackListener listener) {
        try {
            OkHttpUtils.postString().url(Constant.URL_SHOPCAR_EDIT)
                    .addHeader("Authorization","Bearer "+token)
                    .mediaType(MediaType.parse(Constant.json_format))
                    .content(jsonInfo)
                    .build()
                    .execute(new EditShopCarCallback() {
                        @Override
                        public void onResponse(Msg msg, int arg1) {
                            if (listener != null) {
                                listener.onCallBackCompleted(msg);
                            }
                        }

                        @Override
                        public void onError(Call arg0, Exception arg1, int arg2) {
                            super.onError(arg0, arg1, arg2);
                            if (listener != null) {
                                Msg msg = new Msg();
                                msg.setMsg(arg1.toString());
                                listener.onCallBackCompleted(msg);
                            }
                        }
                    });
        } catch (Exception e) {
        }
    }

}
